<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\admin\controller\v1;
use app\admin\Adminbase;
use base\files\Poster;
use app\admin\model\SystemParamValue;

class ActivityDiscount extends Adminbase
{
	
	/**
	* 删除
	* /admin/v1/activitydiscount/del
	* 需要登录 
	* @is_need_login true
	*/
	public function del($ids){
		if(!$ids){
			return $this ->  ReError("参数错误");
		}
		try {
			$result = model('ActivityDiscount')->setDel($ids,1);
			if(!$result){
				return $this->ReError($result);
			}
			return $this->ReSucess(true);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 设置启用与禁用
	* /admin/v1/activitydiscount/setEnable
	* 需要登录 
	* @is_need_login true
	*/
	public function setEnable($ids,$state){
		try {
			$result = model('ActivityDiscount')->setEnable($ids,$state);
			if(!$result){
				return $this->ReError($result);
			}
			return $this->ReSucess(true);	
		} catch (\Exception $e) {
			return $this->ReSucess($e->getMessage());	
		}
	}
	


	


    /**
     * 设置作废
     * /admin/v1/activitydiscount/setStop
     * 需要登录
     * @is_need_login true
     */
    public function setStop($ids,$state){
        try {
            $result = model('ActivityDiscount')->where('id',$ids)->update(['status'=>$state]);

//            if(!$result){
           // return $this->ReError("已经禁用过了！");
////            }
           return $this->ReSucess(true);
        } catch (\Exception $e) {
            return $this->ReSucess($e->getMessage());
        }
    }


    /**
     * 变更活动下的字段商品id
     * /admin/v1/coupon/setCouponGs
     * 需要登录
     * @is_need_login true
     */
    public function setCouponGs($coupon_id,$goodData,$goodcateDate){
        try {
            //拼接

            $goodData=json_decode($goodData,true);
            $goodcateDate=json_decode($goodcateDate,true);

//            $cost = array_unique(array_column($goodData, 'name'));
//            if(count($cost) != count($goodData)){
//                return $this->ReError("选择商品重复，请检查！");
//            }

            $gid_str='';
            foreach ($goodData as $k=>$v) {
                $gid_str=$gid_str.$v['good_id'];
                if($k!=count($goodData)-1)
                    $gid_str=$gid_str.',';
            }

            $gid_cate_str='';
            foreach ($goodcateDate as $k=>$v) {
                $gid_cate_str=$gid_cate_str.$v['cate_id'];
                if($k!=count($goodcateDate)-1)
                    $gid_cate_str=$gid_cate_str.',';
            }

            model('ActivityDiscount')->where('id',$coupon_id)->update(['goods_scope'=>$gid_str]);
            model('ActivityDiscount')->where('id',$coupon_id)->update(['goods_cate_scope'=>$gid_cate_str]);

            return $this->ReSucess(true);

        } catch (\Exception $e) {
            return $this->ReSucess($e->getMessage());
        }
    }







}

